package com.jianshuge.app.bean;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.List;

import com.jianshuge.app.AppException;
import com.jianshuge.app.common.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class BooklistList extends Entity {
	
	public final static int CATALOG_HOT_BOOKLIST = 1;
	public final static int CATALOG_ZERO_BOOKLIST = 2;
	public final static int CATALOG_FOLLOWED_BOOKLIST = 3;
	public final static int CATALOG_CREATED_BOOKLIST = 4;
	public final static int CATALOG_PART_BOOKLIST = 5;
	
	public static final String TYPE_HOT = "hottest";
	public static final String TYPE_ZERO = "zero";
	public static final String TYPE_FOLLOWED = "followed";
	public static final String TYPE_CREATED = "created";
	public static final String TYPE_PART = "part";
	
	private int page_size;
	private int booklist_count;
	
	private List<Booklist> booklist_list = new ArrayList<Booklist>();
	
	public int getPageSize() {
		return page_size;
	}
	public int getBooklistCount() {
		return booklist_count;
	}
	public List<Booklist> getBooklistList() {
		return booklist_list;
	}
	
	// convert InputStream to String
	private static String getStringFromInputStream(InputStream is) {
 
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
 
		String line;
		try {
 
			br = new BufferedReader(new InputStreamReader(is));
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
 
		return sb.toString();
	}

	 public static String to_my_string( InputStream  is) throws IOException, AppException  {
		    int size = is.available();
		    char[] theChars = new char[size];
		    byte[] bytes    = new byte[size];

		    is.read(bytes, 0, size);
		    for (int i = 0; i < size;)
		        theChars[i] = (char)(bytes[i++]&0xff);
		    
		    return new String(theChars);
	}
	 
	public static BooklistList parse(InputStream inputStream) throws IOException, AppException {
		
		BooklistList my_booklist_list = new BooklistList();
		Booklist booklist = null;

		String inputStr;
		BufferedReader streamReader = new BufferedReader( new InputStreamReader(inputStream) ); 
	    StringBuilder responseStrBuilder = new StringBuilder();
	    while ((inputStr = streamReader.readLine()) != null)
	        responseStrBuilder.append(inputStr);
	    String strResult = responseStrBuilder.toString();
		
		try {
			JSONArray json_arr = new JSONArray(strResult);
			
			my_booklist_list.page_size = json_arr.length();
			my_booklist_list.booklist_count = json_arr.length();
			
			for (int i = 0; i < json_arr.length(); i++) {
				
				booklist = new Booklist();
				JSONObject booklist_json_obj = json_arr.getJSONObject(i);
				
				booklist.id = StringUtils.toInt(booklist_json_obj.getString("id"), 0);
				booklist.setTitle(booklist_json_obj.getString("title"));
				booklist.setKeywords(booklist_json_obj.getString("keywords"));
				booklist.setDescs(booklist_json_obj.getString("descs"));
				booklist.setUserId(booklist_json_obj.getString("user_id"));
				booklist.setUserName(booklist_json_obj.getString("user_name"));
				booklist.setUserAvatar(booklist_json_obj.getString("user_avatar"));
				booklist.setBooksCount(booklist_json_obj.getString("books_count"));
				booklist.setViewsCount(booklist_json_obj.getString("views_count"));
				booklist.setFollowersCount(booklist_json_obj.getString("followers_count"));
				booklist.setCommentsCount(booklist_json_obj.getString("comments_count"));
				booklist.setVoteUpCount(booklist_json_obj.getString("vote_up_count"));
				booklist.setCreateTime(booklist_json_obj.getString("create_time"));
				booklist.setUpdateTime(booklist_json_obj.getString("update_time"));
				
				JSONArray booklistitems_json_objs = booklist_json_obj.getJSONArray("booklistitems");
				for (int j = 0; j < booklistitems_json_objs.length(); j++) {
					BooklistItem booklist_item = new BooklistItem();
					JSONObject booklist_item_json_obj = booklistitems_json_objs.getJSONObject(j);
					booklist_item.setBookId( booklist_item_json_obj.optInt("book_id") );
					booklist_item.setTitle( booklist_item_json_obj.getString("book_title") );
					booklist_item.setBookImageUrl( booklist_item_json_obj.getString("book_image_url") );
					booklist_item.setBookRating( booklist_item_json_obj.getString("book_rating") );
					booklist.getBooklistItems().add(booklist_item);
				}
				
				JSONArray contributor_json_objs = booklist_json_obj.getJSONArray("contributors");
				for (int k = 0; k < contributor_json_objs.length(); k++) {
					Contributor contributor = new Contributor();
					JSONObject contributor_json_obj = contributor_json_objs.getJSONObject(k);
					contributor.setUserId( contributor_json_obj.getString("user_id") );
					contributor.setUserName( contributor_json_obj.getString("user_name") );
					contributor.setUserAvatar( contributor_json_obj.getString("user_avatar") );	
					booklist.getContributors().add(contributor);
				}	
				
				my_booklist_list.getBooklistList().add(booklist);
				booklist = null;
				
			}
			
		} catch (JSONException e) { 
            System.out.println("Jsons parse error !"); 
            e.printStackTrace(); 
        }
		
		return my_booklist_list;
	}
}
